package com.ogawa.joypal.activity

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseActivity
import com.ogawa.base.bean.LoadState
import com.ogawa.base.utils.ScreenUtils
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil
import com.ogawa.joypal.R
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.projectcommon.dialog.ShowUpdateDialog
import com.ogawa.projectcommon.utils.ProjectSpUtils
import com.ogawa.projectcommon.utils.SingletonUtils
import com.ogawa.projectcommon.viewmodel.CheckUpdateModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_wel_come.*


@Route(path = UrlPathConstant.ACTIVITY_WEL_COME)
class WelComeActivity : BaseActivity() {
    private var set = AnimatorSet()
//    private var  drawable: pl.droidsonroids.gif.GifDrawable ?= null
    override fun getLayoutId(): Int {
        return R.layout.activity_wel_come
    }
    lateinit var mCheckUpdateModel: CheckUpdateModel
    override fun initView() {
        super.initView()
        mCheckUpdateModel = ViewModelProvider(this).get(CheckUpdateModel::class.java)
        SystemStatusBarUtil.setStatusBar(this, com.ogawa.base.R.color.colorTransparent, true)
        getWindow().navigationBarColor = Color.BLUE
        startGif()
        iv_bg.setOnClickListener {
            stopGif()
            gifEnd()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    private fun stopGif(){
        set.end()
    }
    private fun startGif() {
        set = AnimatorSet()
        var firstSet = AnimatorSet()
        var secondSet = AnimatorSet()
        firstSet.playTogether(
            ObjectAnimator.ofFloat(logo1, "alpha",0.1f,1f),
            ObjectAnimator.ofFloat(logo1, "scaleY",0.1f,1f),
            ObjectAnimator.ofFloat(logo1, "scaleX",0.1f,1f),
            ObjectAnimator.ofFloat(logo2, "alpha",0.1f,1f),
            ObjectAnimator.ofFloat(logo2, "scaleY",0.1f,1f),
            ObjectAnimator.ofFloat(logo2, "scaleX",0.1f,1f)
        )
        secondSet.playTogether(
            ObjectAnimator.ofFloat(feiji, "alpha",0.1f,1f),
            ObjectAnimator.ofFloat(feiji, "scaleX",0.5f,1f),
            ObjectAnimator.ofFloat(feiji, "scaleY",0.5f,1f)
            , ObjectAnimator.ofFloat(feiji, "translationX",ScreenUtils.dip2px(this,50f).toFloat())
        )
        set.playSequentially(firstSet,secondSet)
        set.setDuration(2000);
        set.start()
        set.addListener(object : Animator.AnimatorListener{
            override fun onAnimationRepeat(p0: Animator?) {
            }
            override fun onAnimationEnd(p0: Animator?) {
                LogUtils.e("onAnimationEnd")
                gifEnd()
            }
            override fun onAnimationCancel(p0: Animator?) {
            }
            override fun onAnimationStart(p0: Animator?) {
            }
        })
    }


    private fun gifEnd(){
        //动画播放结束
        AndPermission.with(mActivity)
            .runtime()
            .permission(Permission.Group.STORAGE)
            .onGranted {
                checkUpdate()

            }
            .onDenied {
                ToastUtils.showShort(getString(R.string.quest_per))
                finish()
            }
            .start()

    }

    private fun checkUpdate(){
        mCheckUpdateModel.checkUpdate(mActivity)
        mCheckUpdateModel.mUpdateResponseBean.observe(this, Observer {
//            it.isupgrade =1
//            it.url = "https://dldir1.qq.com/weixin/android/weixin7020android1780_arm64.apk"
//            it.appversion = "123123"
//            it.isforce = "1"
//            it.notice = "超级集客最新版来啦！本次更新:\n" +
//                    "1）获客工具更名换新！四种获客工具更名为“文库”“海报”“推广”“素材”。\n" +
//                    "2）员工跟进客户的联系按钮“打电话”“发消息”“复制昵称”，三合一显示为“联系TA”，点击即可展开，优化繁琐功能展示。\n" +
//                    "3）雷达实时跟踪轨迹列表，采用时间轴的样式，日期与时间分开展示，界面更加清爽明了，助员工随时掌握客户动态。"
            SingletonUtils.get().isNeedUpdateApp = it.isupgrade == 1
            if (it.isupgrade == 1){
                var mShowUpdateDialog = ShowUpdateDialog(mActivity, it,
                    ShowUpdateDialog.ShowUpdateDialogCallBack {
                        it.dismiss()
                        gotoMain()
                    })
                mShowUpdateDialog.show()
            }else{
                gotoMain()
            }

        })
        mCheckUpdateModel.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Loading -> {
                    showLoadingDialog(canceledOnTouchOutside = false)
                }
                is LoadState.Fail -> {
                    dismissLoadingDialog()
                    ToastUtils.showShort(it.msg)
                    LogUtils.e(it.msg)
                    AlertDialog.Builder(mActivity)
                        .setMessage(getString(R.string.check_update_fail))
                        .setNegativeButton(
                            R.string.confirm
                        ) { _, _ ->
                            finish()
                        }
                            .setOnDismissListener {
                                finish()
                            }
                        .show()


                }
                is LoadState.Success -> {
                    dismissLoadingDialog()
                }
            }
        })

    }

    private fun gotoMain(){
        val userInfo = ProjectSpUtils.getUserInfo()
        if (userInfo == null){
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_LOGIN).navigation()
            finish()
            return
        }
        if (userInfo.isVisitor){
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN).navigation()
            finish()
            return
        }
        if (userInfo.token.isBlank()){
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_LOGIN)
                .navigation()
        }else {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN).navigation()
        }
        finish()
    }



}